<?php

namespace app\base\controller;

use app\base\model;
use app\common\controller\AdminBase;

class Role extends AdminBase {
  private $model, $modelRoleUser, $modelRoleMenu;
  static protected
    $ignorePath = ['common',
                   'lang'],
    $ignoreController = ['__construct',
                         '_initialize',
                         'assign',
                         'beforeAction',
                         'display',
                         'engine',
                         'error',
                         'fetch',
                         'getResponseType',
                         'redirect',
                         'result',
                         'success',
                         'validate',
                         'validateFailException'];

  public function __construct() {
    parent::__construct();
    $this->model = new model\Role();
    $this->modelRoleUser = new model\RoleUser();
    $this->modelRoleMenu = new model\RoleMenu();
  }

  public function _initialize() {
    parent::_initialize(); // TODO: Change the autogenerated stub
  }

  public function index() {
    if (!request()->isAjax()) {
      return view();
    }
    $rows = $this->model->order('sort DESC')->field('id,title,desc,sort')->select();
    $this->result($rows, 1);
  }

  public function edit() {
    $id = input('id/d', 0);
    $row = $this->model->find($id);
    if ($id > 0 && !$row) $this->error(lang('data empty'));
    if (!request()->isPost()) {
      return view('', ['row' => $row]);
    }
    $form = input('form/a', NULL);
    $this->model->editJump($form, $id, 'index');
  }

  public function menu() {
    if (!request()->isPost()) {
      $id = input('id/d', 0, 'abs');
      if (!$id) $this->error(lang('id empty'));
      if ($id === 1) $this->error(lang('can not edit admin group'));
      $row = $this->model->find($id);
      if (!$row) $this->error(lang('row empty'));

      return view('',
        ['role_id' => $id,
         'row'     => $row,]
      );
    } else {
      $nodes = input('nodes/a', []);
      $role_id = input('role_id/d', 0);
      $data = [];
      foreach ($nodes as $n) {
        $data[] = ['role_id' => $role_id,
                   'menu_id' => $n];
      }
      $this->modelRoleMenu->saveRoleMenu($data, $role_id);
    }
  }

  public function menuAuth() {
    $role_id = input('role_id/d', 0);
    $db = db('base_role_menu');
    $nodes = $db->where('role_id', $role_id)->column("menu_id");

    //dump($nodes);
    return json(['nodes' => $nodes]);
  }

  public function user() {
    $id = input('id/d', 0);
    if (!$id) $this->error(lang('id empty'));
    $row = $this->model->getById($id);
    if (!$row) $this->error(lang('row empty'));
    //
    if (!request()->isPost()) {
      //获取所有的角色用户
      $rows = $this->modelRoleUser->getRoleUser($id);

      return view('',
        ['rows'      => $rows,
         'uids'      => implode(',', array_column($rows, 'user_id')),
         'realnames' => implode(',', array_column($rows, 'realname')),
        ]
      );
    }
    $form = input('form/a', NULL);
    if (!$form) $this->error(lang('form empty'));
    $this->modelRoleUser->setRoleUser($form, $id);
    $this->success(lang('success'));
  }

  public function auth() {
    //$rows = $model->where('role_id', $id)->column('access');
    $path = glob('../application/*', GLOB_ONLYDIR);
    $controllers = $controllers2 = [];
    foreach ($path as $c) {
      $module = basename($c);
      if (in_array($module, self::$ignorePath)) continue;
      $files = glob($c . '/controller/*.php');
      $classes = str_replace('application', 'app', $files);
      $classes = str_replace('.php', '', $classes);
      $classes = str_replace('/', '\\', $classes);
      $classes = str_replace('..\\', '', $classes);

      foreach ($classes as $class) {
        $controller=array_slice (explode('\\',$class),-1,1);
        $sort = get_class_methods($class);
        sort($sort);
        $controllers[ $module ][$controller[0]] = $sort;
      }
    }
    $rows = [];
    //
    foreach ($controllers as $con => $classes) {
      foreach ($classes as $class) {
        if (in_array($class, self::$ignoreController)) continue;
        $full = strtolower($con . '\\' . $class);
        $controllers2[ $con ][ $class ] = in_array($full, $rows);
      }
    }
    $this->assign('controllers2', $controllers2);

    return view();
  }

  public function delete() {
    $id = input('id/d', 0);
    if (!$id) $this->error(lang('id empty'));
    if ($id === 1) $this->error(lang('can not edit admin group'));
    $this->model->deleteRole($id);
  }
}
